package com.person.monitor;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;

import java.util.ArrayList;
import java.util.List;

public class MonitorServer extends Service {

    List<MonitorInfo> monitorInfoList = new ArrayList<>();

    public static List<BaseProcess> processList = new ArrayList<>();

    MonitorTool monitorTool;

    MonitorThread monitorThread = new MonitorThread();

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        MyLog.D("MonitorServer onCreate");

        monitorTool = new MonitorTool(this);

        processList.add(new WorkVolumeProcess(this,"WorkVolume"));
        processList.add(new AutoLightProcess(this,"AutoLight"));
        processList.add(new AlarmVolumeProcess(this,"AlarmVolume"));
        processList.add(new OpenWechatProcess(this, "OpenWechat"));
        processList.add(new OpenXiaomiProcess(this, "OpenXiaomiCar"));

        monitorThread.start();
        MyLog.D("monitorThread.start()");
        showNotification();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    int NOTICE_ID = 9001;
    private void showNotification(){

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            startForeground(NOTICE_ID, NotifyUtil.getInstance(this).getNotify(this,"MonitorServer", "Monitor is running",MainActivity.class.getName(), null));
        }else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            Notification.Builder builder = new Notification.Builder(this);
            builder.setSmallIcon(R.mipmap.ic_launcher);
            builder.setContentTitle("MonitorServer");
            builder.setContentText("Monitor is running");
            builder.setContentIntent(PendingIntent.getActivity(this,0,new Intent(this,MainActivity.class),PendingIntent.FLAG_IMMUTABLE));
            startForeground(NOTICE_ID, builder.build());
        } else {
            startForeground(NOTICE_ID, new Notification());
        }
    }

    private class MonitorThread extends Thread{
        @Override
        public void run() {
            while (true){
                threadSleep();

                MonitorInfo monitorInfo = monitorTool.getMonitorInfo();
                monitorInfoList.add(0,monitorInfo);
                if(monitorInfoList.size() > 10){
                    monitorInfoList.remove(monitorInfoList.size() - 1);
                }

                for (BaseProcess baseProcess:processList){
                    if(baseProcess.getProcessEnable()) {
                        baseProcess.processMonitor(monitorInfoList);
                    }
                }
            }
        }
    }

    private void threadSleep(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static List<BaseProcess> getProcessList(){
        return processList;
    }
}
